'use strict';
// Copyright 2014 Blueant Inc. All Rights Reserved.

/**
 * @fileoverview 每日任务
 * @author sundbcn@gmail.com (sun debing)
 */

var async = require('async');
var _ = require('lodash');
var moment = require('moment');
var Code = require('../../app/consts/code.js');
var dataApi = require('../../app/libs/dataApi');
var rewardService = require('../../app/services/rewardService');
var PlayerManager = require('../../app/libs/playerManager');

module.exports = function(app) {
    return new Handler(app);
};

var Handler = function(app) {
    this.app = app;
};

/**
 * 获取任务奖励
 */
Handler.prototype.reward = function(msg, session, next) {
    var role_id = session.uid;
    console.log('role_id:', role_id);
    var reward_id_list = [];
    var player_manager = new PlayerManager(role_id);
    var reward_list = [];
    async.waterfall(
        [


            function(cb) {
                player_manager.getModel(
                    ['role', 'bag', 'arena', 'expedition', 'skill', 'build', 'soldier', 'task', 'achieve', 'daily_task', 'tech', 'sevenDayActivity', 'accrued'],
                    cb
                );
            },
            function(cb) {
                var role_model = player_manager.role;
                var bag_model = player_manager.bag;

                // add
                var reward_list = dataApi.property.all();
                _.each(reward_list, function(item) {
                    bag_model.addProperty(item.id, 9999);
                });
                //bag_model.addProperty();

                player_manager.save(cb);
            }
        ],
        function(err) {
            if (!!err) {
                console.error(err);
                return next(null, {
                    code: Code.FAIL
                });
            }

            return next(null, {
                code: Code.OK,
                result: {
                    reward_list: reward_list
                }
            });
        }
    );
};
